package lt.marsrutai.mobile.android.dao;

import java.util.HashMap;
import java.util.Map;


public class ApiRequest {
    
    private String methodName;
    private String scopeFolder;
    
    private Map<String, String> params;
    
    public ApiRequest(String methodName) {
        this.methodName = methodName;
        
        params = new HashMap<String, String>();
    }
    
    public ApiRequest scope(String scopeFolder) {
        this.scopeFolder = scopeFolder;
        return this;
    }
    
    public ApiRequest param(String name, String value) {
        if (null != value) {
            params.put(name, value);
        }
        return this;
    }
    
    public ApiRequest param(String name, boolean value) {
        return param(name, String.valueOf(value));
    }
    
    public ApiRequest param(String name, float value) {
        return param(name, String.valueOf(value));
    }
    
    public ApiRequest param(String name, int value) {
        return param(name, String.valueOf(value));
    }
    
    public ApiRequest param(String name, Double value) {
        if (null != value) {
            return param(name, value.toString());
        }
        else {
            return this;
        }
    }
    
    public String toString() {
        StringBuilder sb = new StringBuilder(APIFunctionConstants.API_URI);
        
        if (null != scopeFolder) {
            sb.append(scopeFolder);
            sb.append("/");
        }
        
        sb.append(methodName);
        
        boolean first = true;
        for (Map.Entry<String, String> entry: params.entrySet()) {
            if (first) {
                sb.append("?");
                first = false;
            }
            else {
                sb.append("&");
            }
            
            sb.append(entry.getKey());
            sb.append("=");
            sb.append(entry.getValue());
        }
        
        return sb.toString();
    }
}
